package com.fortune.cat.resolver;

import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;

import static com.fortune.cat.enums.RateLimiterKeyResolverEnums.PATH;

/**
 * @author zhaozeyang
 * @date 2019/4/26 14:05
 * @description
 */
@Component
public class PathKeyResolver extends AbstractKeyResolver {

    protected final static AntPathMatcher antPathMatcher = new AntPathMatcher();


    @Override
    public Integer getType() {
        return PATH.getType();
    }

    @Override
    protected String doResolve(ServerWebExchange exchange) {
        String path = exchange.getRequest().getPath().value();
        String method = exchange.getRequest().getMethod().name();
        if (method.equalsIgnoreCase(requestRateLimiterConfig.getMethod()) & antPathMatcher.match(requestRateLimiterConfig.getPath(), path)) {
            return method.concat("-").concat(requestRateLimiterConfig.getPath());
        }
        return null;
    }
}
